package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.OrderDealMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.order.OrderDeal;
import com.qingcheng.service.order.OrderDealService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class OrderDealServiceImpl implements OrderDealService {

    @Autowired
    private OrderDealMapper orderDealMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<OrderDeal> findAll() {
        return orderDealMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<OrderDeal> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<OrderDeal> orderDeals = (Page<OrderDeal>) orderDealMapper.selectAll();
        return new PageResult<OrderDeal>(orderDeals.getTotal(),orderDeals.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<OrderDeal> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return orderDealMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<OrderDeal> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<OrderDeal> orderDeals = (Page<OrderDeal>) orderDealMapper.selectByExample(example);
        return new PageResult<OrderDeal>(orderDeals.getTotal(),orderDeals.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public OrderDeal findById(Integer id) {
        return orderDealMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param orderDeal
     */
    public void add(OrderDeal orderDeal) {
        orderDealMapper.insert(orderDeal);
    }

    /**
     * 修改
     * @param orderDeal
     */
    public void update(OrderDeal orderDeal) {
        orderDealMapper.updateByPrimaryKeySelective(orderDeal);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Integer id) {
        orderDealMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Map> orderDealfindByTime(String date1, String date2) {
        return orderDealMapper.orderDealfindByTime(date1,date2);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(OrderDeal.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){

            // id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            // 浏览人数
            if(searchMap.get("browseNumber")!=null ){
                criteria.andEqualTo("browseNumber",searchMap.get("browseNumber"));
            }
            // 下单人数
            if(searchMap.get("ordersNumber")!=null ){
                criteria.andEqualTo("ordersNumber",searchMap.get("ordersNumber"));
            }
            // 订单数
            if(searchMap.get("orderformNumber")!=null ){
                criteria.andEqualTo("orderformNumber",searchMap.get("orderformNumber"));
            }
            // 下单件数
            if(searchMap.get("orderitemNum")!=null ){
                criteria.andEqualTo("orderitemNum",searchMap.get("orderitemNum"));
            }
            // 有效订单数
            if(searchMap.get("orderstatusNumber")!=null ){
                criteria.andEqualTo("orderstatusNumber",searchMap.get("orderstatusNumber"));
            }
            // 下单金额
            if(searchMap.get("orderitemMoney")!=null ){
                criteria.andEqualTo("orderitemMoney",searchMap.get("orderitemMoney"));
            }
            // 退款金额
            if(searchMap.get("refundAmount")!=null ){
                criteria.andEqualTo("refundAmount",searchMap.get("refundAmount"));
            }
            // 付款人数
            if(searchMap.get("orderlogNumber")!=null ){
                criteria.andEqualTo("orderlogNumber",searchMap.get("orderlogNumber"));
            }
            // 付款订单数
            if(searchMap.get("paymentOrderform")!=null ){
                criteria.andEqualTo("paymentOrderform",searchMap.get("paymentOrderform"));
            }
            // 付款件数
            if(searchMap.get("paymentNumber")!=null ){
                criteria.andEqualTo("paymentNumber",searchMap.get("paymentNumber"));
            }
            // 付款金额
            if(searchMap.get("orderitemPayMoney")!=null ){
                criteria.andEqualTo("orderitemPayMoney",searchMap.get("orderitemPayMoney"));
            }
            // 客单价
            if(searchMap.get("averageMoney")!=null ){
                criteria.andEqualTo("averageMoney",searchMap.get("averageMoney"));
            }

        }
        return example;
    }

}
